package cn.piesat.client.utils;


import cn.piesat.client.model.dto.base.algorithm.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReadMapFormatUtil {

    private ReadMapFormatUtil(){}

    private static Logger logger = LoggerFactory.getLogger(ReadMapFormatUtil.class);

    public static DMSClientBean formatDmsClientBean(Map<String,String> readMap){
        DMSClientBean bean = null;
        if(readMap.containsKey("algorithmIdCHN") && readMap.containsKey("algorithmIdENG") && readMap.containsKey("parentSubcategory")
                && readMap.containsKey("subcategory") && readMap.containsKey("algorithmMemo") && readMap.containsKey("execMainFcnFile")) {
            bean = new DMSClientBean();
            AlgorithmBean algorithmBean = JSON.parseObject(JSON.toJSONString(readMap), AlgorithmBean.class);
            bean.setAlgorithmBean(algorithmBean);
            //算法参数
            if(readMap.containsKey("param")) {
                bean.setParamBeans(getAlgorithmParamBeanList(readMap.get("param")));
            }
            //扫描规则
            if(readMap.containsKey("rule")){
                bean.setScanningRulesBeans(getScanningRulesBeanList(readMap.get("rule")));
            }
            if(readMap.containsKey("product")) {
                bean.setProductBeans(getProductBeanList(readMap.get("product")));
            }
            if (readMap.containsKey("defaultFlowFile")) {
                String flowData = readAlgorithmFlow(readMap.get("defaultFlowFile"));
                bean.setAlgorithmFlow(JSON.parse(flowData));
            }
        }
        return bean;
    }

    private static List<AlgorithmParamBean> getAlgorithmParamBeanList(String paramsStr){
        String[] params = paramsStr.split(";");
        List<AlgorithmParamBean> paramList = new ArrayList<>();
        for (String param : params) {
            AlgorithmParamBean paramBean = JSON.parseObject(param, AlgorithmParamBean.class);
            paramList.add(paramBean);
        }
        return paramList;
    }

    private static List<ScanningRules> getScanningRulesBeanList(String rulesStr){
        String[] rules = rulesStr.split(";");
        List<ScanningRules> result = new ArrayList<>();
        for(String rule : rules){
            ScanningRules scanningRules = JSON.parseObject(rule, ScanningRules.class);
            result.add(scanningRules);
        }
        return result;
    }

    private static List<ProductBean> getProductBeanList(String productStr){
        String[] products = productStr.split(";");
        List<ProductBean> productsList = new ArrayList<>();
        for (String product : products) {
            ProductBean productBean = JSON.parseObject(product, ProductBean.class);
            productsList.add(productBean);
        }
        return productsList;
    }

    private static String readAlgorithmFlow(String filePath){
        File flowFile = new File(filePath);
        String value = null;
        if (flowFile.exists()) {
            try {
                value = FileUtils.readFileToString(flowFile, "UTF-8");
            } catch (IOException e) {
                logger.error("failed to read flow file!");
            }
        }
        return value;
    }
}
